package com.duoketech.scrawler;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.duoketech.utils.upload.QiNiuUpload;

import cn.hutool.core.lang.UUID;

public class WxJsoup {

	public static WxActivityBean readContentFromWeiXin(String wxUrl){
		Document doc;
		WxActivityBean bean=new WxActivityBean();
		String content="";
		try {
//			String url = "https://mp.weixin.qq.com/s?src=11&timestamp=1546415650&ver=1317&signature=xNIAfQmsgcj5ooJ9KXnET4IaynkutUt3vaezc7n5UT6Jb2YSwuFmOMuU4RBvQO*lOggCPZZQ3UbiDdzAk9AnXbh89LXBoZuqAYqENvINpDDs0we-ctH*pQ1WuNsqVxqU&new=1";
			String url =wxUrl;
			doc = Jsoup.connect(url).get();
			String name = doc.getElementById("activity-name").text();
			bean.setName(name);
			Element elements= doc.getElementById("js_content");
			Elements img = elements.getElementsByTag("img");
			for(int i=0;i<img.size();i++){
				String imgUrl = img.get(i).attr("data-src");
				//上传至七牛
				String fileName = UUID.fastUUID().toString();
				String newUrl = QiNiuUpload.uploadToQiNiuFromNet(imgUrl, fileName);
				img.get(i).attr("src",newUrl);
			}
			content =  elements.toString();
			bean.setContent(content);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return bean;
	}
	
	public static void main(String[] args) {
		String url = "https://mp.weixin.qq.com/s?src=11&timestamp=1546415650&ver=1317&signature=xNIAfQmsgcj5ooJ9KXnET4IaynkutUt3vaezc7n5UT6Jb2YSwuFmOMuU4RBvQO*lOggCPZZQ3UbiDdzAk9AnXbh89LXBoZuqAYqENvINpDDs0we-ctH*pQ1WuNsqVxqU&new=1";
		WxActivityBean bean = new WxJsoup().readContentFromWeiXin(url);
		System.out.println(bean.getContent());
	}
}
